package com.lr.students.service.impl;

import com.lr.students.dao.ClassessMapper;
import com.lr.students.model.Classess;
import com.lr.students.service.ClassessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;


@Service
public class ClassessServiceImpl implements ClassessService {

    @Autowired
    private ClassessMapper classessMapper;



    @Override
    public PageInfo<Classess> findPage(Classess classess, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(classess);
        //执行搜索
        return new PageInfo<Classess>(classessMapper.selectByExample(example));
    }


    @Override
    public PageInfo<Classess> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Classess>(classessMapper.selectAll());
    }


    @Override
    public List<Classess> findList(Classess classess){
        //构建查询条件
        Example example = createExample(classess);
        //根据构建的条件查询数据
        return classessMapper.selectByExample(example);
    }


    public Example createExample(Classess classess){
        Example example=new Example(Classess.class);
        Example.Criteria criteria = example.createCriteria();
        if(classess !=null){
            // 班级id
            if(!StringUtils.isEmpty(classess.getId())){
                    criteria.andEqualTo("id", classess.getId());
            }
            // 班级名称
            if(!StringUtils.isEmpty(classess.getClassName())){
                    criteria.andEqualTo("className", classess.getClassName());
            }

        }
        return example;
    }


    @Override
    public void delete(Integer id){
        classessMapper.deleteByPrimaryKey(id);
    }


    @Override
    public void update(Classess classess){
        classessMapper.updateByPrimaryKey(classess);
    }


    @Override
    public void add(Classess classess){
        classessMapper.insert(classess);
    }


    @Override
    public Classess findByName(String Name){
        Classess classess = new Classess();
        classess.setClassName(Name);
        return  classessMapper.selectOne(classess);
    }

    @Override
    public List<Classess> findAll() {
        return classessMapper.selectAll();
    }
}
